/* $Id: UserAnnouncementDAOImpl.java 
 * Copyright (C) 2011, TRAKHEES
 *  
 * Revision History
 *
 * Date  			Author 		    	Remarks
 * ---------------------------------------------------------------------------------
 *
 */
package ae.pcfc.etks.online.common.dao.provider;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import ae.pcfc.etks.online.common.dao.UserAnnouncementDAO;
import ae.pcfc.etks.online.common.domain.UserAnnouncements;

@Repository
public class UserAnnouncementDAOImpl extends BaseDAO implements UserAnnouncementDAO {

	
	/**
	 * add
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UserAnnouncementDAO#add(ae.pcfc.etks.online.common.domain.UserAnnouncements)
	 * 
	 */
	public void add(UserAnnouncements userAnnouncements) {
		
		getHibernateTemplate().getSessionFactory().getCurrentSession().save(userAnnouncements);

	}

	/**
	 * findUserAnnouncementsByUserName
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UserAnnouncementDAO#findUserAnnouncementsByUserName(java.lang.String)
	 * 
	 */
	public List<UserAnnouncements> findUserAnnouncementsByUserName(String userName) {
		
		Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(UserAnnouncements.class, "announcement");
		criteria.createAlias("announcement.user", "user");
		criteria.add(Restrictions.eq("user.UUsername", userName));
		criteria.addOrder(Order.desc("UAnnouncementCreateDate"));
		
		@SuppressWarnings("unchecked")
		List<UserAnnouncements> userAnnouncements =  criteria.list();

		return (userAnnouncements != null && userAnnouncements.size() > 0 ) ? userAnnouncements : null;
		
	}
	
	
}
